package com.dingdayu.club.admin;

import android.os.AsyncTask;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dingdayu.club.admin.api.LoginApi;
import com.dingdayu.club.admin.common.util.HttpUtils;
import com.dingdayu.club.admin.common.util.SharePreferenceUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;


public class NoticeActivity extends AppCompatActivity {

    public static Button nButton;
    public static EditText nEditText_title;
    public static EditText nEditText_content;
    public static SharePreferenceUtil spU;

    public static String title;
    public static String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);

        nEditText_title = (EditText)findViewById(R.id.editText);
        nEditText_content = (EditText)findViewById(R.id.editText3);
        nButton = (Button)findViewById(R.id.button);
        spU = new SharePreferenceUtil(LoginApi.USER_INFO_FILE);

        nButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //提交数据
                title = nEditText_title.getText().toString();
                content = nEditText_content.getText().toString();
                sendNoticeTask sendNoticeTask = new sendNoticeTask();
                sendNoticeTask.execute("");
                //Snackbar.make(v, "通知提交成功", Snackbar.LENGTH_LONG).setAction("Action", null).show();
            }
        });



    }

    /**
     * 保存设置
     */
    public class sendNoticeTask extends AsyncTask<String, Integer, Boolean> {

        @Override
        protected Boolean doInBackground(String... params) {
            publishProgress(0);
            HashMap<String, String> hm = new HashMap<String, String>();

            try {
                HashMap<String, String> params2 = new HashMap<String, String>();
                //检查用户登录
                spU = new SharePreferenceUtil(LoginApi.USER_INFO_FILE);
                int ClubID = spU.getClubID();
                if(ClubID != 0){
                    //                String name = params[1];
//                String school = params[2];
                    Log.e("ClubID", String.valueOf(ClubID));
                    params2.put("club_id", String.valueOf(ClubID));
                    params2.put("title", title);
                    params2.put("content", content);
                    publishProgress(18);
                    String t = HttpUtils.submitPostData("http://corp.xyser.com/Home/Api/sendNotice", params2, "utf-8");
                    Log.e("t", t);
                    try {
                        publishProgress(80);
                        JSONObject jsonObj = new JSONObject(t);
                        int code = jsonObj.getInt("code");
                        String msg = jsonObj.getString("msg");
                        if (code == 302) {
                            // new AlertDialog.Builder(LoginActivity.loginactivity).setTitle(msg).setMessage("请重试").setPositiveButton("确定", null).show();
                            return false;
                        }

                        if (code == 200) {
                            return true;
                        }
                    }catch (JSONException e) {
                        //new AlertDialog.Builder(LoginActivity.loginactivity).setTitle("服务器返回错误").setMessage("请稍后重试").setPositiveButton("确定",null).show();
                        Log.e("LoginAPI", "返回数据解析错误" + e.getMessage());
                        //login_ret = false;
                        //e.printStackTrace();
                        return false;
                    }
                }

            } catch (Exception e) {
                return false;
            }
            publishProgress(100);
            return false;
        }

        protected void onProgressUpdate(Integer... progress) {
            //TtextView3.setText("loading..." + progress[0] + "%");
        }

        protected void onPostExecute(Boolean result) {
            if (result) {
                Toast.makeText(getApplicationContext(), "发送成功", Toast.LENGTH_LONG).show();
            } else {
                Log.i("Main","数据错误");
                Toast.makeText(getApplicationContext(), "发送失败", Toast.LENGTH_LONG).show();
            }
        }

        protected void onPreExecute() {
        }

        protected void onCancelled() {
            //mProgressBar.setProgress(0);
        }
    }
}
